import Axios from 'common/js/axios'
import axios from 'axios'
import Config from '@/config'

export function uploadToken(cropWH = '') {
	const url = Config.baseUrl + '/index/ToolFile/uploadSignature'
	let json = {
		cropWH
	}
	return Axios.post(url, json)
}

export function uploadFile(file) {
	return new Promise((resolve, reject) => {
		uploadToken().then(res => {
			let data = res
			var formData = new FormData()
			formData.append("policy", data.filePolicy)
			formData.append("signature", data.fileSignature)
			formData.append("fileBucket", data.fileBucket)
			formData.append("file", file)
			console.log(formData)
			axios({
				method: 'POST',
				url: `${Config.uploadImgUrl}${data.fileBucket}`,
				data: formData,
				headers: {
					'Content-Type': 'multipart/form-data'
				}
			}).then(_data => {
				var fileUrl = data.fileDomain + _data.data.url;
				resolve(fileUrl)
			}).catch(err => {
				reject(err)
			})
		}).catch(err => {
			reject(err)
		})
	})
}
